<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { formRules } from "../utils/rule";
import { FormProps } from "../utils/types";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    id: 0,
    userName: "",
    userPass: "",
    mobile: 0,
    userState: 1
  })
});
import { useRole } from "../utils/hook";
const treeRef = ref();
const tableRef = ref();
const { allCustomer } = useRole(tableRef, treeRef);
const ruleFormRef = ref();
const newFormInline = ref(props.formInline);
console.log("xiugai ", props.formInline);
function getRef() {
  return ruleFormRef.value;
}
const userStateBoolean = ref(true);
onMounted(() => {
  watch(newFormInline, newValue => {
    userStateBoolean.value = newValue.userState === 1;
  });

  watch(userStateBoolean, newValue => {
    newFormInline.value.userState = newValue ? 1 : 0;
  });
});

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="102px"
  >
    <el-form-item v-if="!newFormInline.id" label="用户名称" prop="userName">
      <el-input
        v-model="newFormInline.userName"
        clearable
        placeholder="请输入用户名称"
      />
    </el-form-item>
    <el-form-item v-if="!newFormInline.id" label="用户密码" prop="userPass">
      <el-input
        v-model="newFormInline.userPass"
        clearable
        placeholder="请输入用户密码"
        type="password"
      />
    </el-form-item>

    <el-form-item label="用户电话" prop="mobile">
      <el-input
        v-model="newFormInline.mobile"
        clearable
        placeholder="请输入用户电话"
      />
    </el-form-item>
    <el-form-item label="用户状态" prop="userState ">
      <el-switch v-model="userStateBoolean" />
    </el-form-item>
  </el-form>
</template>
